
/**
 * 闪屏页, 利用的是全局弹出层来实现
 */

import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:moonplay/common/app_common.dart';

class SplashScreen extends StatefulWidget {
  final GestureTapCallback onTap;
  final GestureDoubleTapCallback onDoubleTap;

  const SplashScreen({Key key, this.onTap, this.onDoubleTap}) : super(key: key);

  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  Timer _countdownTimer;
  int _countdownNum = 2;

  @override
  void initState() {
    super.initState();
    reGetCountdown();
  }

  // 不要忘记在这里释放掉Timer
  @override
  void dispose() {
    _countdownTimer?.cancel();
    _countdownTimer = null;
    super.dispose();
  }

  void reGetCountdown() {
    setState(() {
      if (_countdownTimer != null) {
          return;
      }
      _countdownTimer = Timer.periodic(new Duration(seconds: 1), (timer) {
        setState(() {
          if (_countdownNum > 1) {
            _countdownNum--;
          } else {
            widget.onTap();
          }
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
      child: Scaffold(
        body: Container(
          color: Color.fromRGBO(0, 0, 0, 0.9),
          child: Stack(
            children: <Widget>[
              Positioned(
                top: 0,
                left: 0,
                right: 0,
                bottom: 0,
                child: GestureDetector(
                  onTap: (){
                    // widget.onTap();
                  },
                  onDoubleTap: widget.onDoubleTap,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      Text('锦瑟无端五十弦', style: TextStyle(color: Color(0xfffbfbfb)),),
                      Text('一弦一柱思华年', style: TextStyle(color: Color(0xfffbfbfb)),),
                      Text('庄生晓梦迷蝴蝶', style: TextStyle(color: Color(0xfffbfbfb)),),
                      Text('望帝春心托杜鹃', style: TextStyle(color: Color(0xfffbfbfb)),),
                      Container(height: ScreenUtil().getWidthPx(160),)
                    ],
                  ),
                ),
              ),
              // Positioned(
              //   top: 60,
              //   right: 0,
              //   child: GestureDetector(
              //     onTap: widget.onTap,
              //     child: Container(
              //       width: 100,
              //       height: 40,
              //       // color: Colors.blue,
              //       alignment: Alignment.center,
              //       child: Text('跳过(${_countdownNum})', style: TextStyle(fontSize: 14, color: Color(0xfffbfbfb), decoration: TextDecoration.none)),
              //     ),
              //   )
              // ),
            ],
          ),
        ),
      )
    );
  }
}